package com.guanyong.www.maoyanDemo;

import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/21 0021.
 */
public class viodeSourceSQL {

    public static void main(String[] args) {
        Connection conn = MySQLConnect.getConnection();


        String sql = "SELECT f_id, f_sourceurl, f_sourcelocal  FROM t_ad_videosource ";
        PreparedStatement pstmt;
        Map<String,VideoSource> videoSourceMap = new HashMap<String, VideoSource>();
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();

            while (rs.next()) {
                VideoSource videoSource = new VideoSource();
                videoSource.f_id=rs.getString(1);
                videoSource.f_sourceurl=getdecode(rs.getString(2));
                videoSource.f_sourcelocal=getdecode(rs.getString(3));
                videoSourceMap.put(videoSource.f_id,videoSource);
            }
            Statement stmt=conn.createStatement();
            for (String key : videoSourceMap.keySet()) {
                String sql2 = "UPDATE t_ad_videosource SET f_sourceurl='"+videoSourceMap.get(key).f_sourceurl+"',f_sourcelocal='"+videoSourceMap.get(key).f_sourceurl+"' WHERE f_id="+key;
                stmt.executeUpdate(sql2);
            }
            stmt.close();
            MySQLConnect.close(rs,pstmt,conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



    public static String getdecode(String url){
        if (url.indexOf(".com")>0){
            url=url.substring(24);
        }

        String   mytext2=null;
        try {
            mytext2   =   java.net.URLDecoder.decode(url,   "utf-8");
            mytext2=mytext2.replaceAll("\\+", " ");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return mytext2;
    }
    private static class VideoSource{
        public String f_id;
        public String f_sourceurl;
        public String f_sourcelocal;
    }

}
